//
//  WSRegistSView.m
//  GroupFourFM
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 GroupFour. All rights reserved.
//

#import "WSRegistSView.h"
#import <BmobSDK/Bmob.h>

@interface WSRegistSView (){

    NSInteger num;
}

@property (nonatomic, strong) UITextField *pass;

@property (nonatomic, strong) UITextField *passWord;

@property (nonatomic, strong) UITextField *testWord;

@property (nonatomic, strong) UIButton *but;

@property (nonatomic, strong) UIButton *testBtn;

@property (nonatomic, strong) NSTimer *time;

@end

@implementation WSRegistSView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createSubView];
    }
    return self;
}

- (void)createSubView {

    self.backgroundColor = [UIColor colorWithWhite:0.957 alpha:1.000];
    num = 60;
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    CGFloat x = width / 2;
    CGFloat y = height / 3;
    
#pragma mark 返回 1000
    UIButton *returnBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnBut setImage:[UIImage imageNamed:@"iconfont-addcollapse"] forState:UIControlStateNormal];
    [returnBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:returnBut];
    [returnBut mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@20);
        make.top.mas_equalTo(@30);
        make.height.width.mas_equalTo(@32);
    }];
    
#pragma mark 输入框 1010 1011
    UIView *textView = [[UIView alloc] init];
    textView.backgroundColor = [UIColor whiteColor];
    textView.center = CGPointMake(x, y);
    [textView.layer setBorderWidth:1.5];
    [textView.layer setBorderColor:[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1].CGColor];
    [self addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@-1);
        make.top.mas_equalTo(@150);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(@137);
    }];
    
    //密码
    _pass = [[UITextField alloc] init];
    [textView addSubview:_pass];
    [_pass mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@25);
        make.top.mas_equalTo(@0);
        make.right.mas_equalTo(@20);
        make.height.mas_equalTo(@45);
    }];
    
    UIView *aview = [[UIView alloc] init];
    aview.backgroundColor = [UIColor colorWithWhite:0.851 alpha:1.000];
    [textView addSubview:aview];
    [aview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@15);
        make.top.mas_equalTo(@45);
        make.right.mas_equalTo(@-5);
        make.height.mas_equalTo(@1);
    }];
    
    //确认密码
    _passWord = [[UITextField alloc] init];
    [textView addSubview:_passWord];
    [_passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@25);
        make.top.mas_equalTo(@46);
        make.right.mas_equalTo(@20);
        make.height.mas_equalTo(@45);
    }];
    
    UIView *bview = [[UIView alloc] init];
    bview.backgroundColor = [UIColor colorWithWhite:0.851 alpha:1.000];
    [textView addSubview:bview];
    [bview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@15);
        make.top.mas_equalTo(@91);
        make.right.mas_equalTo(@-5);
        make.height.mas_equalTo(@1);
    }];
    
    //验证码
    _testWord = [[UITextField alloc] init];
    [textView addSubview:_testWord];
    [_testWord mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@25);
        make.top.mas_equalTo(@92);
        make.width.equalTo(textView).multipliedBy(0.6);
        make.height.mas_equalTo(@45);
    }];
    
    //获取验证码
    _testBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [textView addSubview:_testBtn];
    [_testBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_testWord.mas_right);
        make.top.mas_equalTo(@92);
        make.right.mas_equalTo(@0);
        make.height.mas_equalTo(@45);
    }];
    [self.testBtn setTitleColor:[UIColor colorWithRed:0.73 green:0.76 blue:0.79 alpha:1] forState:UIControlStateNormal];
    [_testBtn addTarget:self action:@selector(testAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    NSArray *array = [NSArray arrayWithObjects:@" 密码", @" 确认密码", @" 验证码", nil];
    NSArray *textFieldArr = [NSArray arrayWithObjects:_pass, _passWord, _testWord, nil];
    
    for (int i = 0; i < 3; i++) {
        
        UITextField *textfield = textFieldArr[i];
        textfield.placeholder = array[i];
        
        if (i < 2) {
            textfield.secureTextEntry = YES;
        }
    }
    
#pragma mark 登录键 1003
    self.but = [UIButton buttonWithType:UIButtonTypeCustom];
    self.but.tag = 1003;
    [self.but.layer setCornerRadius:5];
    [self.but setTitle:@"注册" forState:UIControlStateNormal];
    [self.but addTarget:self action:@selector(registAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_but];
    [self.but mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self);
        make.top.equalTo(textView.mas_bottom).offset(20);
        make.height.mas_equalTo(@40);
        make.width.equalTo(self).multipliedBy(0.8);
    }];
    self.but.backgroundColor = [UIColor colorWithRed:0.082 green:0.565 blue:0.847 alpha:1.000];
    
    self.time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTitle) userInfo:nil repeats:YES];
    [_testBtn setUserInteractionEnabled:NO];
}

- (void)changeTitle {

    if (num != 0) {
        [self.testBtn setTitle:[NSString stringWithFormat:@"%ld后重新获取", num] forState:UIControlStateNormal];
        num--;
        [self.testBtn setTitleColor:[UIColor colorWithRed:0.73 green:0.76 blue:0.79 alpha:1] forState:UIControlStateNormal];
        [_testBtn setUserInteractionEnabled:NO];
    }
    else {
    
        [self.testBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.testBtn setTitleColor:[UIColor colorWithRed:0.11 green:0.55 blue:0.96 alpha:1] forState:UIControlStateNormal];
        [_testBtn setUserInteractionEnabled:YES];
        [self.time invalidate];
        self.time = nil;
    }
    
}

- (void)buttonAction:(UIButton *)button {

    [self.delegate backViewController];
}

- (void)testAction:(UIButton *)button {

    [self.delegate getTestWord];
    self.time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTitle) userInfo:nil repeats:YES];
}

- (void)registAction:(UIButton *)button {
    
    NSString *password = _pass.text;
    NSString *passWord = _passWord.text;
    
    [self.delegate registWithPassWord:passWord repassWord:password testWord:_testWord.text];
    
}

@end
